package com.zlzkj.app.controller;

import java.util.HashMap;
import java.text.ParseException;
import java.text.SimpleDateFormat;
//import java.util.List;
import java.util.Map;
import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
//import javax.servlet.http.HttpSession;

import com.zlzkj.app.model.PlanItem;
import com.zlzkj.app.util.IDGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zlzkj.core.base.BaseController;
//import com.zlzkj.app.service.UserService;
import com.zlzkj.app.service.GroupService;
import com.zlzkj.app.aop.Log;
import com.zlzkj.app.model.Plan;
import com.zlzkj.app.service.EnergytypeService;
import com.zlzkj.app.service.PlanService;
import com.zlzkj.app.service.PlanItemService;
//import com.zlzkj.app.service.UserService;
import com.zlzkj.app.util.MapUtil;
import com.zlzkj.app.util.PageView;

@Controller
@RequestMapping(value = "/plan")
public class PlanController extends BaseController{

    @Value("${PAGE_SIZE}")
    private int PAGE_SIZE;

    @Value("${FILE_REPOSITORY}")
    private String FILE_REPOSITORY;

    @Autowired
    private GroupService groupService;

    @Autowired
    private PlanService planService;

    @Autowired
    private EnergytypeService energytypeService;

    @Autowired
    private PlanItemService planItemService;

    @RequestMapping(value = "/index")
    public String home(Model model, HttpServletRequest request, HttpServletResponse response, Integer nowPage){
        Map map = MapUtil.convert(request.getParameterMap());
        model.addAttribute("pageView", new PageView(planService.findByMap(map, nowPage)));
        return "plan/index";
    }

    @Log(name="新增计划组数据")
    @RequestMapping(value = {"/create"}, method = RequestMethod.POST)
    public String create(Model model, HttpServletRequest request, HttpServletResponse response, Plan plan,
                         String startTime1,String stopTime1,String[] clectPlanitemEnergyId,
                         String[] clectPlanitemAmount) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        plan.setId(IDGenerator.generator());
        plan.setStartTime(sdf.parse(startTime1));
        plan.setStopTime(sdf.parse(stopTime1));
        planService.save(plan);
        if (clectPlanitemEnergyId != null){
            for (int i=0; i<clectPlanitemEnergyId.length;i++){
                PlanItem planItem = new PlanItem();
                planItem.setPlanid(plan.getId());
                planItem.setEnergyid(clectPlanitemEnergyId[i]);
                planItem.setAmount(clectPlanitemAmount[i]);
                planItemService.save(planItem);
            }
        }
        return ajaxReturn(response, 1);
    }

    @ResponseBody
    @Log(name="删除计划组数据")
    @RequestMapping(value = {"/delete"}, method = RequestMethod.POST)
    public String delete(Model model, HttpServletRequest request, HttpServletResponse response, String[] id) {
        planService.deletebyIds(id);
        planItemService.deleteByPlanIds(id);
        return ajaxReturn(response,1);
    }

    @Log(name="修改计划组数据")
    @RequestMapping(value = {"/update"}, method = RequestMethod.POST)
    public String edit(Model model, HttpServletRequest request, HttpServletResponse response, Plan plan,
                       String startTime1,String stopTime1,String[] clectPlanitemEnergyId,
                       String[] clectPlanitemAmount) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        plan.setStartTime(sdf.parse(startTime1));
        plan.setStopTime(sdf.parse(stopTime1));
        planService.update(plan);
        planItemService.deleteByPlanId(plan.getId());
        if (clectPlanitemEnergyId != null){
            for (int i=0; i<clectPlanitemEnergyId.length;i++){
                PlanItem planItem = new PlanItem();
                planItem.setPlanid(plan.getId());
                planItem.setEnergyid(clectPlanitemEnergyId[i]);
                planItem.setAmount(clectPlanitemAmount[i]);
                planItemService.save(planItem);
            }
        }
        return ajaxReturn(response, 1);
    }

    //json 计划列表
    @ResponseBody
    @Log(name="查看计划组数据")
    @RequestMapping(value = {"/list"}, method = RequestMethod.POST)
    public Map<String, Object> planList(HttpServletRequest request, Integer nowPage) {
        Map<String, Object> result = new HashMap();
        Map map = MapUtil.convert(request.getParameterMap());
        result.put("pageView", new PageView(planService.findByMap(map, nowPage)));
        return result;
    }

    @ResponseBody
    @RequestMapping(value = {"/to_create"}, method = RequestMethod.POST)
    public Map<String, Object> toCreate(HttpServletRequest request) {
        Map<String, Object> result = new HashMap();
        //result.put("obj",new Plan());
        result.put("groups", groupService.findByMap(new HashMap<String, Object>()));
        result.put("energy", energytypeService.findByMap(new HashMap<String, Object>()));
        return result;
    }
    @ResponseBody
    @RequestMapping(value = {"/to_create2"}, method = RequestMethod.POST)
    public Map<String, Object> toCreate2(HttpServletRequest request,String ids) {
        Map<String, Object> result = new HashMap();
        String demand = groupService.findById(ids).getDemand();
        String[] ss = demand.split("_");
        result.remove("energy");
        ArrayList list = new ArrayList();
        for (int i = 0; i < ss.length; i++) {
            list.add(energytypeService.findById(ss[i]));
        }
        result.put("energy", list);
        return result;
    }
    @ResponseBody
    @RequestMapping(value = {"/to_create3"}, method = RequestMethod.POST)
    public Map<String, Object> toCreate3(HttpServletRequest request,String ids) {
        Map<String, Object> result = new HashMap();
        ArrayList list = new ArrayList();
        list.add(energytypeService.findById(ids));
        //result.remove("energy");
        result.put("energy", list);
        return result;
    }

    @ResponseBody
    @RequestMapping(value = {"/to_edit"}, method = RequestMethod.POST)
    public Map<String, Object> toEdit(HttpServletRequest request, String id) {
        Map<String, Object> result = new HashMap();
        result.put("obj", planService.findById(id));
        result.put("groups", groupService.findByMap(new HashMap<String, Object>()));
        result.put("planitems", planItemService.findByPlanId(id));
        String demand = groupService.findById(planService.findById(id).getGroupId()).getDemand();
        String[] ss = demand.split("_");
        result.remove("energy");
        ArrayList list = new ArrayList();
        for (int i = 0; i < ss.length; i++) {
            list.add(energytypeService.findById(ss[i]));
        }
        result.put("energy", list);
        return result;
    }

//     @RequestMapping(value = {"/getName"}, method = RequestMethod.POST)
//     public String getName(Model model, HttpServletRequest request, HttpServletResponse response, Integer nowPage) {
//         List<Energytype> lists = energytypeService.selectName();
//         return ajaxReturn(response,lists);
//     }
//     @ResponseBody
//     @RequestMapping(value = "/check_username")
//     public boolean checkAccount(String name, String originalname, HttpSession session) {
//         Map<String, Object> paraMap = new HashMap();
//         paraMap.put("username", name);
//         if (userService.findByMap(paraMap).size() > 0 && !name.equals(originalname)) {
//             return true;
//         }
//         return false;
//     }

}
